package com.readbook.action;

import com.readbook.service.UaccountService;
import com.readbook.util.ExcelUtils;
import com.readbook.util.OIDCardUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@EnableAutoConfiguration
@RequestMapping("/uaccount")
public class uaccount {
    @Resource
    private UaccountService uaccountService;


    @RequestMapping("Search_uaccount.html")
    @ResponseBody
    public Map<String,Object>Search_uaccount(HttpServletRequest request, HttpServletResponse response){
        String name="";
        if(request.getParameter("name")!=null){
            name=request.getParameter("name").trim();
        }
        String dlzh="";
        if(request.getParameter("dlzh")!=null){
            dlzh=request.getParameter("dlzh").trim();
        }

        String nj="";
        if(request.getParameter("nj")!=null){
            nj=request.getParameter("nj").trim();
        }

        String bj="";
        if(request.getParameter("bj")!=null){
            bj=request.getParameter("bj").trim();
        }

        String wstate="";
        if(request.getParameter("wstate")!=null){
            wstate=request.getParameter("wstate").trim();
        }
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("name",name);
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("dlzh",dlzh);
        gmap.put("wstate",wstate);
        List<Map<String,Object>>list1=uaccountService.Search_uaccount(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }


    @RequestMapping("refreshUaccount.html")
    @ResponseBody
    public Map<String,Object> refreshUaccount(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>getm=uaccountService.refreshUaccount();
        return getm;
    }



    @RequestMapping("insertUaccount.html")
    @ResponseBody
    public Map<String,Object> insertUaccount(HttpServletRequest request, HttpServletResponse response){
        String dlzh=request.getParameter("dlzh");
        String name=request.getParameter("name");
        String wstate=request.getParameter("wstate");
        String sex=request.getParameter("sex");
        String zw=request.getParameter("zw");
        String nj=request.getParameter("nj");
        String bj=request.getParameter("bj");
        String year=request.getParameter("year");
        Map<String,Object>setmap=new HashMap<>();
        setmap.put("usid",UUID.randomUUID().toString().replaceAll("-", ""));
        setmap.put("dlzh",dlzh);
        setmap.put("dlma","123456");
        setmap.put("name",name);
        setmap.put("wstate",wstate);
        setmap.put("sex",sex);
        setmap.put("zw",zw);
        setmap.put("nj",nj);
        setmap.put("bj",bj);
        setmap.put("year",year);
        Map<String,Object>getm=uaccountService.Insert_uaccount(setmap);
        return getm;
    }




    @RequestMapping("Edit_uaccount.html")
    @ResponseBody
    public Map<String,Object> Edit_uaccount(HttpServletRequest request, HttpServletResponse response){
        String usid=request.getParameter("usid");
        String dlzh=request.getParameter("dlzh");
        String name=request.getParameter("name");
        String wstate=request.getParameter("wstate");
        String sex=request.getParameter("sex");
        String zw=request.getParameter("zw");
        String nj=request.getParameter("nj");
        String bj=request.getParameter("bj");
        String year=request.getParameter("year");
        Map<String,Object>setmap=new HashMap<>();
        setmap.put("usid",usid);
        setmap.put("dlzh",dlzh);
        setmap.put("name",name);
        setmap.put("wstate",wstate);
        setmap.put("sex",sex);
        setmap.put("zw",zw);
        setmap.put("nj",nj);
        setmap.put("bj",bj);
        setmap.put("year",year);
        Map<String,Object>getm=uaccountService.Edit_uaccount(setmap);
        return getm;
    }



    @RequestMapping("del_uaccount.html")
    @ResponseBody
    public Map<String,Object> del_uaccount(HttpServletRequest request, HttpServletResponse response){
        String usid=request.getParameter("usid");
        Map<String,Object>getm=uaccountService.del_uaccount(usid);
        return getm;
    }


    //升级
    @RequestMapping("upgrade_uaccount.html")
    @ResponseBody
    public Map<String,Object> upgrade_uaccount(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>getm=uaccountService.upgrade_uaccount();
        return getm;
    }


    @RequestMapping("import_Uaccount.html")
    @ResponseBody
    public Boolean import_Uaccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String,Object>>lsitdata=ExcelUtils.getExcelUaccounts(request);
        int x=0;
        for(int i=0;i<lsitdata.size();i++){
            Map<String,Object>maps=lsitdata.get(i);
            String sname="";
            if(maps.get("学生姓名")!=null){
                sname=maps.get("学生姓名").toString().trim();
            }else{
                continue;
            }
            String sfzh="";
            if(maps.get("身份证件号")!=null){
                sfzh=maps.get("身份证件号").toString().trim();
                boolean va= OIDCardUtil.validateCard(sfzh);
                if(!va){
                    continue;
                }
            }else{
                continue;
            }

            Calendar cal = Calendar.getInstance();
            int year=cal.get(Calendar.YEAR);
            System.out.println(year);
            if(maps.get("年级")!=null){
                String njstr=maps.get("年级").toString();
                String regEx="[^0-9]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(njstr);
                year=Integer.parseInt(m.replaceAll("").trim());
            }else{
                continue;
            }
            int month = cal.get(Calendar.MONTH) + 1;//月
            String nowyear =cal.get(Calendar.YEAR)+"";
            int nj = 1;
            if(month>8){
                System.out.println(nowyear);
                nj=(Integer.parseInt(nowyear) - year)+1;
            }else{
                nj=Integer.parseInt(nowyear) - year;
            }

            String bj="";
            if(maps.get("班级")!=null){
                String bjstr=maps.get("班级").toString().trim();
                int strStartIndex = bjstr.indexOf("级")+1;
                int strEndIndex = bjstr.indexOf("班");
                /* 开始截取 */
                bj = bjstr.substring(strStartIndex, strEndIndex);
            }else{
                continue;
            }

            String jzsj="";
            if(maps.get("联系电话")!=null){
                jzsj=maps.get("联系电话").toString().trim();
            }else{
                continue;
            }


            //判断身份证是否正确
            Map<String,Object>gmap=new HashMap<>();
            gmap.put("nj",nj);
            gmap.put("bj",bj);
            gmap.put("name",sname);
            gmap.put("sfzh",sfzh);
            gmap.put("year",year);
            String sex=OIDCardUtil.getGenderByIdCard(sfzh);
            gmap.put("sex",sex);
            gmap.put("wstate","1"); // 1.学生 2.教师
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            gmap.put("usid",uuid);
            gmap.put("dlzh",jzsj);
            gmap.put("dlma","123456");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            gmap.put("jlxzsj", df.format(new Date()));
            gmap.put("jlsczt", "0");
            gmap.put("lasttime", System.currentTimeMillis());

            if(!sfzh.equals("")){
                Map<String,Object>getm=uaccountService.Insert_uaccount(gmap);
                if(getm.get("result").toString().equals("true")){
                    x++;
                }
            }

        }
        if(x>0){
            return true;
        }else{
            return false;
        }
    }


}
